<?php
require_once dirname(__FILE__) . "/../config/config.conf.php";
require_once OA_LIB_PATH . "/inner/service/libraries/ServiceBase.php";
require_once dirname(__FILE__) . "/InterfaceLog.php";

interface IService
{
    public function open ($args, $env = null);
    public function close ($args, $env = null);
    public function update ($args, $env = null);
    public function get ($args, $env = null);
    public function sync ($args, $env = null);
}
/*interface IError
{
    public function getErrNo();
    
    public function getErrMsg();
    
    protected function setErrNo($errNo);
    
    protected function setErrMsg($errMsg); 
}*/
class InterfaceBase
{
    // 开发环境
    const APP_ENV_TEST = 1; // 测试环境
    const APP_ENV_PRODUCTION = 2; // 正式环境
    protected static $ENVType = array(
        self::APP_ENV_TEST => SDK_ENV_TEST, 
        self::APP_ENV_PRODUCTION => SDK_ENV_ONLINE
    );
    /*protected $_commands = array();
    
    protected $_observers = array();
    
    protected static $_models = array();*/
    protected $errNo = 0;
    protected $errMsg = '';
    protected static $_sdks = array();
    protected $env;
    protected static $logger = array();
    public function __construct ($env = self::APP_ENV_TEST)
    {
        $this->env = $env;
    }
    public function setEnv ($env)
    {
        if (! isset(self::$ENVType[$env])) {
            $this->setError(SDOServiceErrorCode::API_EC_PARAM, "Invalid Pamaters");
            return false;
        }
        $this->env = $env;
        return true;
    }
    public static function loadSdk ($key)
    {
        if (! isset(self::$_sdks[$key])) {
            self::$_sdks[$key] = require_once OA_LIB_PATH . '/inner/service/' . $key . '.php';
        }
    }
    /**
     * write log
     *
     * @param string $s
     * @param string $server
     * @param string $level
     */
    protected function log ($s, $server, $level = "info")
    {
        if (! isset(self::$logger[$server]) || ! (self::$logger[$server] instanceof InterfaceLog)) {
            self::$logger[$server] = InterfaceLog::getLogInstance($server);
        }
        self::$logger[$server]->$level($s);
    }
    public function getErrNo ()
    {
        return $this->errNo;
    }
    public function getErrMsg ()
    {
        return $this->errMsg;
    }
    protected function setError ($errNo, $errMsg)
    {
        $this->errNo = $errNo;
        $this->errMsg = $errMsg;
    }
	/**
     * 设置数组参数默认值
     * @param array $arr 数组
     * @param array $need 需要填写默认值的变量
     * @param mixed $default 默认值
     * 
     * @return void
     */
    private function setDefault (&$arr, $need, $default = 0)
    {
        foreach ($need as $k) {
            if(!isset($arr[$k]) || $arr[$k]===""){
                $arr[$k] = $default;    
            }
  
        }
    }
    
    protected function getSDKEnvType($env=null)
    {
    	if ($env) {    		
	    	if (isset(self::$ENVType[$env])) {
	    		$env = self::$ENVType[$env];
	    	} elseif ($env != SDK_ENV_TEST){
	    		$env = SDK_ENV_ONLINE;
	    	} else {
	    		$env = SDK_ENV_TEST;
	    	}
    	} else {
    		$env = SDK_ENV_TEST;
    	}
    	return $env;
    }
    
    protected function startLog($args, $env, $method, $class)
    {
    	$this->setError(0, '');
    	$errMsg = array('START'=>'Action Atart:', SDK_LOG_METHOD=>$method, SDK_LOG_ENV=>$env, SDK_LOG_PARAMS=>json_encode($args));
    	$this->log(buildErr($errMsg, false, false), $class, 'info');
    }
    
    protected function endLog($class)
    {
    	$errMsg = array('END'=>'Action END:','code'=>$this->getErrNo(), 'message'=>$this->getErrMsg());
    	$this->log(buildErr($errMsg, false, false), $class, 'info');
    }
    
  	protected function processLog($class, $op)
  	{
  		$errMsg = array('END'=>'Processing:','op'=>$op);
    	$this->log(buildErr($errMsg, false, false), $class, 'info');
  	}
}
class InterfaceException extends Exception
{
	
}
